from OWRpy import * 
import redRGUI 
import redRi18n
_ = redRi18n.get_(package = 'base')


class homoscedasticity(OWRpy): 
    globalSettingsList = ['commit']
    def __init__(self, **kwargs):
        OWRpy.__init__(self, **kwargs)
        
        # Python variables
        self.data = {}
        self.RFunctionParam_object = ''

        #create R variables in the R session. 
        self.setRvariableNames(["hsTest"])
        
        # Input
        self.inputs.addInput('id0', _('R Variable Object'), signals.base.RDataFrame, self.processobject)

        # GUI
        self.area = redRGUI.base.widgetBox(self.controlArea,orientation='vertical')  
        
        self.testType = redRGUI.base.comboBox(self.area,  label = "Test type: ",
        items = [('bartlett','Bartlett'),('levene','Levene')],
        editable=False)

        self.testOptions = redRGUI.base.widgetBox(self.area, orientation='vertical')
        self.testOptionsLists = redRGUI.base.widgetBox(self.testOptions, orientation='horizontal')
        self.namesListVar = redRGUI.base.listBox(self.testOptionsLists, label = _('Variable to test'))
        self.namesListVar.setSelectionMode(QAbstractItemView.SingleSelection)
        self.namesListGroup = redRGUI.base.listBox(self.testOptionsLists, label = _('Grouped by'))
        self.namesListGroup.setSelectionMode(QAbstractItemView.SingleSelection)
        
        
        # Commit
        self.commit = redRGUI.base.commitButton(self.bottomAreaRight, _("Commit"), alignment=Qt.AlignLeft, 
        callback = self.commitFunction, processOnInput=True)
        self.RoutputWindow = redRGUI.base.textEdit(self.controlArea, label = _("RoutputWindow"))
        
    def processobject(self, data):
        if not self.require_librarys(["car"]):
            self.status.setText('R Libraries Not Loaded.')
            return
        #if not self.require_librarys(["stats"]):
            #self.status.setText('R Libraries Not Loaded.')
            #return
        
        if data:
            self.RFunctionParam_object=data.getData()
            self.data = data
            self.namesListVar.update(self.R('names('+self.RFunctionParam_object+')', wantType = 'list'))
            self.namesListGroup.update(self.R('names('+self.RFunctionParam_object+')', wantType = 'list'))
            if self.commit.processOnInput():
                self.commitFunction()
        else:
            self.RFunctionParam_object=''

    def commitFunction(self):
        if unicode(self.RFunctionParam_object) == '':
            self.status.setText('No data')
            return
        if len(self.namesListVar.selectedItems()) == 0:
            self.status.setText('Must select a variable')
            return   
        if len(self.namesListGroup.selectedItems()) == 0:
            self.status.setText('Must select a group')
            return   
        
        self.R('varSelected<-'+unicode(self.RFunctionParam_object)+'[,'+unicode(self.namesListVar.row(self.namesListVar.currentItem())+1)+']')
        self.R('groupSelected<-factor('+unicode(self.RFunctionParam_object)+'[,'+unicode(self.namesListGroup.row(self.namesListGroup.currentItem())+1)+'])')
    
    
        if self.testType.currentId() == 'bartlett':
            self.R(self.Rvariables['hsTest']+'<-bartlett.test(x=varSelected, g=groupSelected)') 
        elif self.testType.currentId() == 'levene':
            self.R(self.Rvariables['hsTest']+'<-leveneTest(y=varSelected, g=groupSelected)') 
        
        self.RoutputWindow.clear()
        self.RoutputWindow.insertHtml('<pre>'+str(self.R(self.Rvariables['hsTest']))+'</pre>')
        
